package com.guigu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import com.guigu.user.entity.Member;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.user.entity.query.MemberQuery;
import com.guigu.user.mapper.MemberMapper;
import com.guigu.user.service.MemberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-09
 */
@Service
@CacheConfig(cacheNames = "member")

public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {


    @Cacheable(key = "#query.phone+#query.idCardNoVerify+#current+#pageSize")
    @Override
    public R<IPage<Member>> queryByNameOfPage(MemberQuery query, Integer pageSize, Integer current) {
        if (ObjectUtils.isEmpty(query))
            throw new TmsException(ResultCode.ERROR_CODE, "参数query为null");

        if (pageSize <= 0)
            pageSize = 5;
        if (current <= 0)
            current = 1;
        IPage<Member> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(current);
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        if (query.getIdCardNoVerify() != null) {
            queryWrapper.eq("id_card_no_verify", query.getIdCardNoVerify());
        }

        queryWrapper.like("phone", query.getPhone());
        IPage<Member> selectPage = this.baseMapper.selectPage(page, queryWrapper);
        return new R<>(ResultCode.SUCCESS_CODE, selectPage);
    }

    @Override
    @CacheEvict(allEntries = true)
    public R checkMemberById(String id) {
        Member member = new Member();
        member.setId(id);
        member.setIdCardNoVerify(true);
        int i = this.baseMapper.updateById(member);
        return i > 0 ? new R(ResultCode.SUCCESS_CODE, "审核成功!", true) : new R(ResultCode.ERROR_CODE, "审核失败!", false);
    }
}
